package com.easyder.wrapper.base.view;

import android.Manifest;
import android.content.Intent;

import com.easyder.wrapper.R;
import com.easyder.wrapper.base.presenter.MvpBasePresenter;
import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.Permission;
import com.luck.picture.lib.permissions.RxPermissions;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Auther:  winds
 * Data:    2017/4/19
 * Desc:    图片选择
 */

public abstract class WrapperPickerActivity<P extends MvpBasePresenter> extends WrapperSwipeActivity<P> {

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    obtainMediaResult(PictureSelector.obtainMultipleResult(data));
                    break;
            }
        }
    }
    /**
     * 选取图片
     *
     * @param maxSelectNum 选取图片的数量  多选时才生效
     * @param singleMode   是否单选
     * @param showCamera   是否显示拍照按钮
     * @param crop         是否剪裁
     * @param isCircleMode 剪裁是否显示圆形参考   用于裁剪圆形头像    开启裁剪时生效
     */
    public void showPictureSelector(int maxSelectNum, boolean singleMode, boolean showCamera, boolean crop, boolean isCircleMode) {
        PictureSelectionModel model = PictureSelector.create(mActivity)
                .openGallery(PictureMimeType.ofImage()) //查看类型  仅图片,可选择其它或全部
                .theme(R.style.picture_default_style)
                .maxSelectNum(maxSelectNum) //最大图片选择数量
                .imageSpanCount(4)// 每行显示个数
                .minSelectNum(1)// 最小选择数量
                .selectionMode(singleMode ?
                        PictureConfig.SINGLE : PictureConfig.MULTIPLE)// 单选 or 多选
                .isCamera(showCamera);// 是否显示拍照按钮


        if (crop) {
            model
                    .enableCrop(crop)             //开启裁剪
                    .hideBottomControls(false)  //是否隐藏裁剪工具栏
                    .circleDimmedLayer(isCircleMode)    //显示圆形参考图层
                    .withAspectRatio(1, 1);     //裁剪比例
        }
        model.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /**
     * 默认不压缩的选择图库
     * @param maxSelectNum
     * @param singleMode
     * @param showCamera
     */
    public void showPictureSelector(int maxSelectNum, boolean singleMode, boolean showCamera) {
        showPictureSelector(maxSelectNum, singleMode, showCamera, false, false);
    }

    /**
     * 获取到选择的内容的回调
     * @param list
     */
    public abstract void obtainMediaResult(List<LocalMedia> list);

    void requestPermission() {
        // 清空图片缓存，包括裁剪、压缩后的图片 注意:必须要在上传完成后调用 必须要获取权限
        RxPermissions permissions = new RxPermissions(this);
        permissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {

                    }
                });
        permissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Boolean aBoolean) {
                if (aBoolean) {

                } else {
                    showToast("权限被拒绝");
                }
            }

            @Override
            public void onError(Throwable e) {
                showToast("申请权限出错");
            }

            @Override
            public void onComplete() {
                showToast("申请权限onComplete");
            }
        });
    }
}

